'use strict';

const Controller = require('egg').Controller;

const {createToken}=require("../utils/utils")

class HomeController extends Controller {
  // 获取列表
  async getList(){
      const {ctx}=this;
      const result=await ctx.service.user.listService()
      if(result.length){
        ctx.body={
          code:"1001",
          msg:"获取数据成功",
          result
        }
      }else{
        ctx.body={
          code:"1002",
          msg:"获取数据失败"
        }
      }
  }
  //登录
  async login() {
    const { ctx } = this;
    const {username,pwd}=ctx.request.body
    //调用service
    const result=await ctx.service.user.loginService(username,pwd)
    if(result.length){
      const token=createToken(result[0].id)
      ctx.body={
        code:"1001",
        msg:"登录成功",
        token
      }
    }else{
      ctx.body={
        code:"1002",
        msg:"登录失败"
      }
    }
  }
}

module.exports = HomeController;
